package com.example.smartcity1;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.bumptech.glide.Glide;
import com.example.smartcity1.bean.MyLoginBean;
import com.example.smartcity1.bean.MyLoginPostBean;
import com.example.smartcity1.bean.MyUserInfoBean;
import com.example.smartcity1.network.RetrofitClient;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.smartcity1.network.Utils.login;


public class MainActivity extends AppCompatActivity {

    SharedPreferences user;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        NavController navController = Navigation.findNavController(this,R.id.fragment);
        AppBarConfiguration configuration = new AppBarConfiguration.Builder(bottomNavigationView.getMenu()).build();
        NavigationUI.setupActionBarWithNavController(this,navController,configuration);
        NavigationUI.setupWithNavController(bottomNavigationView,navController);
        user = getSharedPreferences("user",MODE_PRIVATE);
        if (!user.getString("username", "").equals("")) {
            login();
        }
    }
    //登录
    public void login() {
        RetrofitClient.appService().getMyLoginData(new MyLoginPostBean(user.getString("username",""),user.getString("password",""))).enqueue(new Callback<MyLoginBean>() {
            @Override
            public void onResponse(Call<MyLoginBean> call, Response<MyLoginBean> response) {
                MyLoginBean loginBean = response.body();
                if (loginBean != null) {
                    if (loginBean.getCode() == 200) {
                        login = 1;
                        SharedPreferences.Editor editor = user.edit();
                        editor.putString("token",loginBean.getToken());
                        editor.apply();
                        getUserId();
                    }
                }
            }
            @Override
            public void onFailure(Call<MyLoginBean> call, Throwable t) {
            }
        });
    }
    private void getUserId() {
        RetrofitClient.appService().getMyUserInfoData(user.getString("token", "")).enqueue(new Callback<MyUserInfoBean>() {
            @Override
            public void onResponse(Call<MyUserInfoBean> call, Response<MyUserInfoBean> response) {
                MyUserInfoBean myUserInfoBean = response.body();
                if (myUserInfoBean != null && myUserInfoBean.getCode() == 200) {
                    MyUserInfoBean.UserDTO userDTO = myUserInfoBean.getUser();
                    SharedPreferences.Editor edit = user.edit();
                    edit.putString("userId", userDTO.getUserId().toString());
                    edit.apply();
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<MyUserInfoBean> call, Throwable t) {
            }
        });
    }
}